<?php
namespace Admin\Model;
use Think\Model;

class UserModel extends Model
{
    protected $tableName = "user";
    protected $tablePrefix = "dangjian_";
    protected $showRow = 5;
    //查询所有用户
    public function queryAllUser ($data)
    {
        $name = $data['name'];
        $group = $data['group'];
        $category = $data['category'];
        $order = $data['order'];//排序字段
        $order1 = $data['order1'];//排序方式
        $map = array();
        if(empty($name)){
        }else{
            $map['u.name'] = array("LIKE", '%' . $name . '%');
        }
        if(empty($group)){
        }else{
            $map['u.group_id'] = $group;
        }
        if(empty($category)){
        }else{
            $map['category_id'] = $category;
        }

        if(empty($order)){
            $order = "u.id";
        }

        if(empty($order1)){
            $order1 = "asc";
        }
        //查询用户总数
        $count = $this->alias("u")
            ->join("dangjian_user_category uc on u.id = uc.user_id")
            ->join("dangjian_category c on c.id = uc.category_id")
            ->join("left join dangjian_group g on g.id=u.group_id")
            ->where($map)->count();
        $page = new \Think\Page($count,$this->showRow);
        $show = $page->show();
        $list = $this->alias("u")
            ->join("dangjian_user_category uc on u.id = uc.user_id")
            ->join("dangjian_category c on c.id = uc.category_id")
            ->join("left join dangjian_group g on g.id=u.group_id")
            ->field("u.id,u.name,u.level,c.name category_name,g.group_name")
            ->where($map)
            ->order("$order $order1")
            ->limit($page->firstRow.','.$page->listRows)
            ->select();
        $res['show'] = $show;
        $res['list'] = $list;

        return $res;
    }

    //根据用户id 查询用户详情
    public function queryUser ($id)
    {
        $res = $this->alias("u")
            ->join("dangjian_user_category uc on u.id = uc.user_id")
            ->join("dangjian_category c on c.id = uc.category_id")
            ->join("left join dangjian_group g on g.id = u.group_id")
            ->field("u.id, u.name, u.level, c.name category_name, u.sex, u.status, u.join_time, u.card_num, u.fee_time,u.tel,g.group_name")
            ->where("u.id=$id")
//            ->fetchSql()
            ->find();
//        echo '<pre>';
//        print_r($res);die;
        return $res;
    }

    public function saveUser ($data,$where)
    {
        $id = $where['id'];
        $data['update_time'] = date("Y-m-d H:i:s");
        $res = $this->where("id=$id")->save($data);
        return $res;
    }

    //根据微信唯一id 查询用户信息
    public function queryUserByOpenId($openId)
    {
        $res = $this->field("id,name,fee_time,card_num")->where("openId=$openId")->find();
        return $res;
    }

    //查询所有党员显示在手机端
    public function queryAllUserByPhone()
    {
        $res = $this->alias("u")
            ->join("left join dangjian_user_category uc on uc.user_id = u.id")
            ->join("left join dangjian_category c on c.id = uc.category_id")
            ->join("left join dangjian_group g on g.id=u.group_id")
            ->field("u.name,c.name category_name,g.group_name")
            ->select();
        return $res;
    }

    //添加用户到数据库
    public function addUser ($data)
    {
        $res =  $this->add($data);
        return $res;
    }
}